package com.zx.filter;/**
 * Author:JasonZ
 * Data:2023/4/17
 */

import com.zx.constant.AuthorityConstant;
import com.zx.context.AccessContext;
import com.zx.model.vo.LoginUserInfo;
import com.zx.util.TokenParseUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: ecommerce-parent
 *
 * @description:
 *
 * @author: JasonZ
 *
 * @create: 2023-04-17 16:47
 **/

public class AccessInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //白名单处理
        //处理请求头
        String token = request.getHeader(AuthorityConstant.AUTHENTICATION_TOKEN_KEY);
        //解析token
        LoginUserInfo loginUserInfo = TokenParseUtil.parseUserInfoFromToken(token);
        //全局网关的过滤器已经校验过token了 所以这里就不用再校验了
        //存入上下文
        AccessContext.setUserContext(loginUserInfo);
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
